package br.edu.sistemaacademico.strategy;

import java.util.regex.PatternSyntaxException;

import javax.swing.DefaultRowSorter;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

public class J6SortLister implements Strategy{

	RowSorter<DefaultTableModel> sorter;

	@Override
	public void sort(JTable tabela, DefaultTableModel model) {
		sorter = new TableRowSorter<DefaultTableModel>(model);
		tabela.setRowSorter(sorter);		
	}
	
	public void filter(final JTextField filterText){
		String text = filterText.getText();
		if (text.length() == 0) {
			((DefaultRowSorter<DefaultTableModel, Integer>) sorter)
					.setRowFilter(null);
		} else {
			try {
				((DefaultRowSorter<DefaultTableModel, Integer>) sorter)
						.setRowFilter(RowFilter.regexFilter(text));
			} catch (PatternSyntaxException pse) {
				System.err.println("Erro");
			}
		}
	}
	
	public void delete(int row){
		if(row==0)
		sorter.rowsDeleted(0, row);
		else if(row>0)
		sorter.rowsDeleted(row-1, row);
	}

}
